package com.business.web.spring;


import com.auth.domain.T_user;
import com.business.domain.*;
import com.business.service.*;
import com.common.db.JsonVO;
import com.common.db.Pg;
import com.common.utils.*;
import com.system.service.LxConstans;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.atomic.DoubleAccumulator;

@Controller
public class FiledownloadController {
    private static Logger log = Logger.getLogger(FiledownloadController.class.getName());

    @Value("${kindeditor.storage.image.path}")  //注入　　private String filePath;
    private String filePath;

    //实现Spring Boot 的文件下载功能，映射网址为/download
    @RequestMapping("/download")
    public void downloadFile(String filename, HttpServletRequest request,
                               HttpServletResponse response) {
            //throws Exception {
        String realPath = filePath;

        // 如果文件名不为空，则进行下载
        if (filename != null) {
            //设置文件路径
            File file = new File(realPath, filename);

            // 如果文件名存在，则进行下载
            if (file.exists()) {

                // 配置文件下载
                response.setHeader("content-type", "application/octet-stream");
                response.setContentType("application/octet-stream");
                // 下载文件能正常显示中文
                try {
                    response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    log.error("------设置下载文件头出错："+e);
                }

                // 实现文件下载
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    log.debug("Download the file successfully! filename=" + filename);
                } catch (Exception e) {
                    log.error("Download the file failed! filename=" + filename);
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }else{
                log.error("------要下载的文件不存在："+realPath + filename);
            }
        }
        //return null;
    }

}
